package IC;

import java.io.File;

public class ArgumentHandler {

	private String[] args; 
	
	private final int MAX_ARGS_ALLOWED = 4;
	
	/************** Files paths **************/
	private String ICFilePath = null;
	private String libraryFilePath = null;
	private String ICFileName = null;
	private String LibraryFileName = null;
	
	/************** flags **************/
	private boolean error = false;
	private boolean libraryFlag = false;
	private boolean printAstFlag = false;
	private boolean debugFlag = false;
	private boolean dumpSymTabFlag = false;
	private boolean printLirFlag = false;
	
	
	public ArgumentHandler(String[] args){
		this.args = args;
		if (!isValidNumberOfArgs()){
			error = true;
		}
		else{
			error = validateArguments();
		}
	}
	
	private boolean validateArguments(){
		ICFilePath = args[0];
		boolean error = false;
		
		for (int i = 1; i < args.length; i++){
			if (isLibraryPathFlag(args[i])){
				setLibraryPathFlag(args[i].substring(2));
			} else if (isPrintAstFlag(args[i])){
				setPrintAstFlag();
			} else if (isDumpSymTabFlag(args[i])){
				setDumpSymTabFlag();
			}else if (isDebugFlag(args[i])){
				setDebugFlag();
			}else if (isPrintLirFlag(args[i])){
				setPrintLirFlag();
			} else{
				error = true;
				break;
			}
		}
		
		if (!hasLibraryFlag()){
			libraryFlag = true;
			libraryFilePath = "libic.sig";
		}
		return error;
	}
	
	/************** flags checkers **************/
	private boolean isLibraryPathFlag(String arg){
		return ((libraryFlag == false) && arg.startsWith("-L"));
	}
	
	private boolean isPrintAstFlag(String arg){
		return ((printAstFlag == false) && arg.equals("-print-ast"));
	}
	
	private boolean isDumpSymTabFlag(String arg){
		return ((dumpSymTabFlag == false) && arg.equals("-dump-symtab"));
	}
	
	private boolean isDebugFlag(String arg){
		return ((debugFlag == false) && arg.equals("-debug"));
	}
	
	private boolean isPrintLirFlag(String arg){
		return ((printLirFlag == false) && arg.equals("-print-lir"));
	}
	
	
	
	/************** flags set-ers **************/
	private void setLibraryPathFlag(String path){
		libraryFlag = true;
		libraryFilePath = path; 
	}
	
	private void setPrintAstFlag(){
		printAstFlag = true;
	}
	
	private void setDumpSymTabFlag(){
		dumpSymTabFlag = true;
	}
	
	private void setDebugFlag(){
		debugFlag = true;
	}
	
	private void setPrintLirFlag(){
		printLirFlag = true;
	}
	
	/************** flags get-ers **************/
	public boolean hasPrintAstFlag(){
		return printAstFlag;
	}
	
	public boolean hasDumpSymTabFlag() {
		return dumpSymTabFlag;
	}
	
	public boolean hasLibraryFlag(){
		return libraryFlag;
	}
	
	public boolean hasDebugFlag(){
		return debugFlag;
	}	
	
	public boolean hasPrintLirFlag(){
		return printLirFlag;
	}
	
	public boolean hasError(){
		return error;
	}
	
	/************** paths checkers **************/
	public boolean checkICFile(){
		if (checkFile(ICFilePath)){
			setICName();
			return true;
		} else {
			return false;
		}
	}
	
	public boolean checkLibraryFile(){
		if (checkFile(libraryFilePath)){
			setLibraryName();
			return true;
		} else {
			return false;
		}
	}
	
	private boolean checkFile(String path){
		File file = new File(path);
		if (!file.exists()){
			return false;
		}else{
			return true;
		}
	}
	
	/************** paths getters **************/
	public String getICFilePath(){
		return ICFilePath;
	}
	
	public String getLibraryFilePath(){
		return libraryFilePath;
	}
	
	public String getICName(){
		return ICFileName;
	}
	
	public String getLibraryName(){
		return LibraryFileName;
	}
	
	
	private boolean isValidNumberOfArgs(){
		int nOfArgs ;
		if (args == null)
			return false;
		else {
			nOfArgs = args.length;
			return ((nOfArgs <= MAX_ARGS_ALLOWED) && (nOfArgs >= 1));
		}
	}
	
	//TODO: add new flags
	public String getUsageMessage(){
		return "Usage: <ic file> [-L</path/to/libic.sig>] [-print-ast] [dump-symtab] [print-lir]\n";
	}	
	
	private void setICName(){
		File file = new File(ICFilePath);
		ICFileName = file.getName();
	}
		
	private void setLibraryName(){
		File file = new File(libraryFilePath);
		LibraryFileName = file.getName();
	}
}
